from django.http import Http404, HttpResponse
from django.db.models.loading import get_app
from os import path

__all__ = ['static_file']

def static_file(request, appname, filepath):
	app = get_app(appname)
	if not app:
		raise Http404('No such app: %s' % (app,))
	models = path.dirname(app.__file__)
	approot = path.dirname(models)
	staticdir = path.join(approot, 'static')
	filepath = path.join(staticdir, filepath)
	try:
		fp = open(filepath)
	except:
		raise Http404('No such file or directory')
	data = fp.read()
	fp.close()
	return HttpResponse(data)
